package com.to.aboomy.bannersample.util;

import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.Matrix;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Rect;

/**
 * @author xujianhong
 * @since 2021-07-27
 */
public class ImageUtils {
    private ImageUtils(){
    }

    public static PixelMap fastBlur(final PixelMap src,
                                   final float scale,
                                    final float radius) {
        return fastBlur(src, scale, radius, false, false);
    }

    /**
     * Return the blur bitmap fast.
     * <p>zoom out, blur, zoom in</p>
     *
     * @param src           The source of bitmap.
     * @param scale         The scale(0...1).
     * @param radius        The radius(0...25).
     * @param recycle       True to recycle the source of bitmap, false otherwise.
     * @param isReturnScale True to return the scale blur bitmap, false otherwise.
     * @return the blur bitmap
     */
    public static PixelMap fastBlur(final PixelMap src,
                                  final float scale,
                                  final float radius,
                                  final boolean recycle,
                                  final boolean isReturnScale) {
        if (isEmptyBitmap(src)) return null;
        int width = src.getImageInfo().size.width;
        int height = src.getImageInfo().size.height;
        Matrix matrix = new Matrix();
        matrix.setScale(scale, scale);


        Rect rect = new Rect();
        rect.minX = 0;
        rect.minX = 0;
        rect.width = src.getImageInfo().size.width;
        rect.height = src.getImageInfo().size.height;

        PixelMap.InitializationOptions opts = new PixelMap.InitializationOptions();
        PixelMap scaleBitmap = PixelMap.create(src,rect,opts);

        Paint paint = new Paint();
        Canvas canvas = new Canvas();
//        PorterDuffColorFilter filter = new PorterDuffColorFilter(
//                Color.TRANSPARENT, PorterDuff.Mode.SRC_ATOP);
//        paint.setColorFilter(filter);
        canvas.scale(scale, scale);

        PixelMapHolder holder = new PixelMapHolder(scaleBitmap);
        canvas.drawPixelMapHolder(holder,0,0,paint);
        scaleBitmap = stackBlur(scaleBitmap, (int) radius, recycle);

        if (scale == 1 || isReturnScale) {
            if (recycle && !src.isReleased() && scaleBitmap != src) src.release();
            return scaleBitmap;
        }
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        options.size.width = width;
        options.size.height = height;
        //.createScaledBitmap(scaleBitmap, width, height, true);
        PixelMap ret = PixelMap.create(scaleBitmap,options);
        if (!scaleBitmap.isReleased()) scaleBitmap.release();
        if (recycle && !src.isReleased() && ret != src) src.release();
        return ret;
    }

    private static boolean isEmptyBitmap(final PixelMap src) {
        return src == null || src.getImageInfo().size.width == 0 || src.getImageInfo().size.height == 0;
    }


    /**
     * Return the blur bitmap using stack.
     *
     * @param src     The source of bitmap.
     * @param radius  The radius(0...25).
     * @param recycle True to recycle the source of bitmap, false otherwise.
     * @return the blur bitmap
     */
    public static PixelMap stackBlur(final PixelMap src, int radius, final boolean recycle) {

        PixelMap.InitializationOptions opts = new PixelMap.InitializationOptions();
        opts.size = src.getImageInfo().size;
        opts.pixelFormat = src.getImageInfo().pixelFormat;
        opts.alphaType = src.getImageInfo().alphaType;
        PixelMap pixelMap = PixelMap.create(opts);

        PixelMap ret = recycle ? src : pixelMap;

        if (radius < 1) {
            radius = 1;
        }
        int w = ret.getImageInfo().size.width;
        int h = ret.getImageInfo().size.height;

        int[] pix = new int[w * h];
        Rect rect = new Rect();
        rect.minX = 0;
        rect.minY = 0;
        rect.width = w;
        rect.height = h;
        ret.readPixels(pix, 0, w, rect);

        int wm = w - 1;
        int hm = h - 1;
        int wh = w * h;
        int div = radius + radius + 1;

        int r[] = new int[wh];
        int g[] = new int[wh];
        int b[] = new int[wh];
        int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
        int vmin[] = new int[Math.max(w, h)];

        int divsum = (div + 1) >> 1;
        divsum *= divsum;
        int dv[] = new int[256 * divsum];
        for (i = 0; i < 256 * divsum; i++) {
            dv[i] = (i / divsum);
        }

        yw = yi = 0;

        int[][] stack = new int[div][3];
        int stackpointer;
        int stackstart;
        int[] sir;
        int rbs;
        int r1 = radius + 1;
        int routsum, goutsum, boutsum;
        int rinsum, ginsum, binsum;

        for (y = 0; y < h; y++) {
            rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
            for (i = -radius; i <= radius; i++) {
                p = pix[yi + Math.min(wm, Math.max(i, 0))];
                sir = stack[i + radius];
                sir[0] = (p & 0xff0000) >> 16;
                sir[1] = (p & 0x00ff00) >> 8;
                sir[2] = (p & 0x0000ff);
                rbs = r1 - Math.abs(i);
                rsum += sir[0] * rbs;
                gsum += sir[1] * rbs;
                bsum += sir[2] * rbs;
                if (i > 0) {
                    rinsum += sir[0];
                    ginsum += sir[1];
                    binsum += sir[2];
                } else {
                    routsum += sir[0];
                    goutsum += sir[1];
                    boutsum += sir[2];
                }
            }
            stackpointer = radius;

            for (x = 0; x < w; x++) {

                r[yi] = dv[rsum];
                g[yi] = dv[gsum];
                b[yi] = dv[bsum];

                rsum -= routsum;
                gsum -= goutsum;
                bsum -= boutsum;

                stackstart = stackpointer - radius + div;
                sir = stack[stackstart % div];

                routsum -= sir[0];
                goutsum -= sir[1];
                boutsum -= sir[2];

                if (y == 0) {
                    vmin[x] = Math.min(x + radius + 1, wm);
                }
                p = pix[yw + vmin[x]];

                sir[0] = (p & 0xff0000) >> 16;
                sir[1] = (p & 0x00ff00) >> 8;
                sir[2] = (p & 0x0000ff);

                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];

                rsum += rinsum;
                gsum += ginsum;
                bsum += binsum;

                stackpointer = (stackpointer + 1) % div;
                sir = stack[(stackpointer) % div];

                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];

                rinsum -= sir[0];
                ginsum -= sir[1];
                binsum -= sir[2];

                yi++;
            }
            yw += w;
        }
        for (x = 0; x < w; x++) {
            rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
            yp = -radius * w;
            for (i = -radius; i <= radius; i++) {
                yi = Math.max(0, yp) + x;

                sir = stack[i + radius];

                sir[0] = r[yi];
                sir[1] = g[yi];
                sir[2] = b[yi];

                rbs = r1 - Math.abs(i);

                rsum += r[yi] * rbs;
                gsum += g[yi] * rbs;
                bsum += b[yi] * rbs;

                if (i > 0) {
                    rinsum += sir[0];
                    ginsum += sir[1];
                    binsum += sir[2];
                } else {
                    routsum += sir[0];
                    goutsum += sir[1];
                    boutsum += sir[2];
                }

                if (i < hm) {
                    yp += w;
                }
            }
            yi = x;
            stackpointer = radius;
            for (y = 0; y < h; y++) {
                // Preserve alpha channel: ( 0xff000000 & pix[yi] )
                pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];

                rsum -= routsum;
                gsum -= goutsum;
                bsum -= boutsum;

                stackstart = stackpointer - radius + div;
                sir = stack[stackstart % div];

                routsum -= sir[0];
                goutsum -= sir[1];
                boutsum -= sir[2];

                if (x == 0) {
                    vmin[y] = Math.min(y + r1, hm) * w;
                }
                p = x + vmin[y];

                sir[0] = r[p];
                sir[1] = g[p];
                sir[2] = b[p];

                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];

                rsum += rinsum;
                gsum += ginsum;
                bsum += binsum;

                stackpointer = (stackpointer + 1) % div;
                sir = stack[stackpointer];

                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];

                rinsum -= sir[0];
                ginsum -= sir[1];
                binsum -= sir[2];

                yi += w;
            }
        }

        Rect rect2 = new Rect();
        rect2.minX = 0;
        rect2.minY = 0;
        rect2.width = w;
        rect2.height = h;

        ret.readPixels(pix, 0, w, rect2);
        return ret;
    }
}
